package com.solomiao.demo.multi.thread.demo8;

import java.util.concurrent.*;

/**
 * @desc 使用ExecutorService提交Callable任务，阻塞获取返回值；使用FutureTask提交Callable任务，阻塞获取返回值；
 * */
public class MainDemo8 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        {
            // 创建一个固定大小的线程池
            ExecutorService executor = Executors.newFixedThreadPool(3);
            System.out.println("线程池已启动，开始提交任务...");
            try {
                Future<Long> future1 = executor.submit(new CallableDemo8());
                Future<Long> future2 = executor.submit(new CallableDemo8());
                Future<Long> future3 = executor.submit(new CallableDemo8());

                System.out.println("任务 1 提交成功，返回值：" + future1.get());
                System.out.println("任务 2 提交成功，返回值：" + future2.get());
                System.out.println("任务 3 提交成功，返回值：" + future3.get());
            } catch (Exception e) {
                System.out.println("提交任务出现异常");
                e.printStackTrace();
            }
            executor.shutdown();
        }

        {
            FutureTask<Long> futureTask = new FutureTask<>(new CallableDemo8());
            new Thread(futureTask).start();

            Long result = futureTask.get(); // 阻塞获取返回值
            System.out.println("返回值：" + result);
        }
    }


}
